package com.dhh.toodledo.activity;

import com.dhh.toodledo.R;
import com.dhh.toodledo.exception.InValidAccountException;
import com.dhh.toodledo.util.AuthUtil;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity implements View.OnClickListener{
	private AuthUtil au;
	private ProgressDialog progressDialog;
	private SharedPreferences sp;
	private EditText email;
	private EditText password;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.login);
		sp = getSharedPreferences("myPre", 0);
        au = new AuthUtil(sp);
        initView();
	}

	@Override
	public void onClick(View v) {
		if(v.getId()==R.id.login_submit){
			String et = email.getText().toString();
			String pt = password.getText().toString();
			if(et!=null && et.trim().length()>0 && pt!=null&& pt.trim().length()>0){
				showDialog(0);
				new SearchThread().start();
			}else{
				Toast.makeText(getApplicationContext(),"Email or Password cannot be empty !", Toast.LENGTH_LONG).show();
			}
			
		}
		
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		switch(id){
			case 0:
				progressDialog = new ProgressDialog(this);
				progressDialog.setTitle("Signing in ....");
				progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
				return progressDialog;			
		}
		
		return null;		
	}
	
	@Override
	public void onBackPressed(){
	}
	
	private void initView(){
		findViewById(R.id.login_submit).setOnClickListener(this);
		email = (EditText) findViewById(R.id.login_user);
    	password = (EditText) findViewById(R.id.login_password);
		if(sp.getString("email", null)!=null)
			email.setText(sp.getString("email", null));
		if(sp.getString("password", null)!=null)
			password.setText(sp.getString("password", null));
			
	}
	
	private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
        	Intent intent = new Intent(LoginActivity.this,TDAActivity.class);
        	LoginActivity.this.startActivity(intent);
            progressDialog.dismiss();
        }
    };
    
    private Handler eHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
			LoginActivity.this.findViewById(R.id.login_error_info).setVisibility(View.VISIBLE);
            progressDialog.dismiss();
        }
    };
    
    private class SearchThread extends Thread {
        @Override
        public void run() {
        	try {
        		au.login(email.getText().toString(), password.getText().toString());
				handler.sendEmptyMessage(0);
			} catch (InValidAccountException e) {
				eHandler.sendEmptyMessage(0);
			}
        	
        }
    }
}
